<?php

namespace app\library;


use Phalcon\Mvc\User\Component;
use Hashids\Hashids;


class Number extends Component
{
    /**
     * ShortUrl constructor.
     * @param \Phalcon\DiInterface $di
     */
    public function __construct(\Phalcon\DiInterface $di)
    {
        $this->setDI($di);
    }

    /**
     * 获取实例
     * @param \Phalcon\DiInterface $di
     * @return static
     */
    public static function getInstance(\Phalcon\DiInterface $di){
        return new static($di);
    }
    /**
     * 获取 hashids 对象
     * @return Hashids
     */
    private function getHashids()
    {
        $config = $this->getDI()->getConfig()->hash;
        return new Hashids($config->salt, $config->length, $config->alphabet);
    }

    /**
     * 数字编码
     * @param $num
     * @return string
     */
    public function numberEncode($num)
    {
        $hash = $this->getHashids();
        return $hash->encode($num);
    }

    /**
     * 数字解码
     * @param $val
     * @return array
     */
    public function numberDecode($val)
    {
        $hash = $this->getHashids();
        return $hash->decode($val);
    }
}